package com.ikas.autumn.iec104.common.baseEntity;

import com.ikas.autumn.iec104.common.exception.BusinessException;
import com.ikas.autumn.iec104.common.util.MessageResolver;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Constructor;


/**
 * Controller公共组件
 */
@Slf4j
public abstract class AbstractBean {

	public void checkBlank(String param, String msg) {
		if (StringUtils.isBlank(param)) {
			tr(BusinessException.class, StringUtils.isBlank(msg) ? "error.param.empty" : msg);
		}
	}

	public void checkNull(Object param,String msg){
		if(param == null){
			tr(BusinessException.class, StringUtils.isBlank(msg) ? "error.param.empty" : msg);
		}
	}

	private void tr(Class<? extends BusinessException> clz, String messageCode, String... codes) throws RuntimeException {
		BusinessException throwable;
		try {
			String message = MessageResolver.getMessages(messageCode, (Object[]) codes);
			Constructor<? extends BusinessException> constructor = clz.getConstructor(String.class);
			throwable = constructor.newInstance(message);
		} catch (Exception e) {
			throwable = new BusinessException(messageCode);
		}
		throw throwable;
	}

}
